package com.inventory.controllers;

import com.inventory.entities.ItemWeight;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 * WeightControllerBean handles the CRUD functionality of the Item_Weight
 * relation.
 * @author sithum
 */
@Stateless(mappedName="WeightController")
public class WeightControllerBean implements WeightControllerRemote {

    /*
     * Injects the specified Persistence unit to the EntityManager associated
     * with the current transaction.
     */
    @PersistenceContext(unitName = "SaicssPU")
    private EntityManager em;

    /**
     * Creates a new set of records using the provided entity instances.
     * @param java.util.List weightList - ItemWeight instace list.
     */
    public void create(List<ItemWeight> weightList) {
        for (ItemWeight weight : weightList){
            em.persist(weight);
            em.flush();
        }
    }

    /**
     * Updates existing set records.
     * @param weightList
     */
    public void edit(List<ItemWeight> weightList){
        for (ItemWeight weight : weightList){
            em.persist(em.merge(weight));
        }
    }

    /**
     * Removes set of records from the Item_Weight relation.
     * @param weightList
     */
    public void delete(List<ItemWeight> weightList){
        for (ItemWeight weight : weightList){
            em.remove(em.merge(weight));
        }
    }

    /**
     * Returns all the records in Item_Weight relation.
     * @return
     */
    public List<ItemWeight> findAll(){
        return em.createNamedQuery("ItemWeight.findAll").getResultList();
    }

    public int count(){
        Query count = em.createNamedQuery("ItemWeight.recordCount");
        return Integer.valueOf(count.getSingleResult().toString()).intValue();
    }

}
